package com.moonshow.earthlivepush.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.moonshow.earthlivepush.R;
import com.moonshow.earthlivepush.View.SplashScreen;
import com.moonshow.earthlivepush.model.Constants;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;

import static com.moonshow.earthlivepush.fragment.PusherVideoQualityFragment.SP_KEY_ROOM_NAME;
import static com.moonshow.earthlivepush.fragment.PusherVideoQualityFragment.SP_KEY_VIDEO_IS_FRONT;
import static com.moonshow.earthlivepush.fragment.PusherVideoQualityFragment.SP_KEY_VIDEO_QUALITY;
import static com.moonshow.earthlivepush.fragment.PusherVideoQualityFragment.SP_KEY_VIDEO_STREAM;
import static com.moonshow.earthlivepush.fragment.PusherVideoQualityFragment.SP_NAME;


public class LiveMainActivity extends Activity {

    private TextView tv_push_set;
    private TextView tv_video_set;
    private SharedPreferences sp;
    private String roomName;
    private String roomUrl;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SplashScreen.show(this, 2000);
        sp = getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        setContentView(R.layout.activity_live_main);
        LinearLayout ll_live_push = findViewById(R.id.ll_live_push);
        LinearLayout ll_live_method = findViewById(R.id.ll_live_method);
        tv_push_set = findViewById(R.id.tv_push_set);
        tv_video_set = findViewById(R.id.tv_video_set);

        ll_live_push.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LiveMainActivity.this, PushSetActivity.class));
            }
        });
        ll_live_method.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LiveMainActivity.this, VideoSetActivity.class));
            }
        });
        findViewById(R.id.tv_start).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(roomName)) {
                    ToastUtils.showShort("直播名称地址不能为空");
                    return;
                }
                if (TextUtils.isEmpty(roomUrl)) {
                    ToastUtils.showShort("直播地址不能为空");
                    return;
                }

                if (checkPublishPermission()) {
                    startToPush();
                }
            }
        });
    }

    private void startToPush() {
        Intent intent = new Intent(LiveMainActivity.this, CameraPushMainActivity.class);
        intent.putExtra(Constants.INTENT_URL_PUSH, roomUrl);
        intent.putExtra(Constants.INTENT_URL_PLAY_RTMP, "rtmp");
        intent.putExtra(Constants.INTENT_URL_PLAY_FLV, "flv");
        intent.putExtra(Constants.INTENT_URL_PLAY_HLS, "hls");
        intent.putExtra(Constants.INTENT_URL_PLAY_ACC, "acc");
        startActivity(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();

        roomName = sp.getString(SP_KEY_ROOM_NAME, "");
        roomUrl = sp.getString(SP_KEY_VIDEO_STREAM, "");
        StringBuilder sbb = new StringBuilder();
        sbb.append(TextUtils.isEmpty(roomName) ? "直播名称未设置" : "直播名称：" + roomName).append("，");
        sbb.append(TextUtils.isEmpty(roomUrl) ? "直播地址未设置" : "直播地址：" + roomUrl);
        tv_push_set.setText(sbb.toString());

        boolean isFront = sp.getBoolean(SP_KEY_VIDEO_IS_FRONT, false);
        int mQualityIndex = sp.getInt(SP_KEY_VIDEO_QUALITY, 1);

        StringBuilder sb = new StringBuilder("横屏，");
        sb.append(isFront ? "前" : "后").append("置摄像头，");
        String q = "";
        if (mQualityIndex == 0) {
            q = "蓝光";
        } else if (mQualityIndex == 1) {
            q = "超清";
        } else if (mQualityIndex == 2) {
            q = "高清";
        } else if (mQualityIndex == 3) {
            q = "标清";
        }
        sb.append(q);
        tv_video_set.setText(sb.toString());
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (REQUEST_CODE == requestCode) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) startToPush();
        }
    }

    private static final int REQUEST_CODE = 100;

    private boolean checkPublishPermission() {
        if (Build.VERSION.SDK_INT >= 23) {
            List<String> permissions = new ArrayList<>();
            if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
            }
            if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA)) {
                permissions.add(Manifest.permission.CAMERA);
            }
            if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)) {
                permissions.add(Manifest.permission.RECORD_AUDIO);
            }
            if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)) {
                permissions.add(Manifest.permission.READ_PHONE_STATE);
            }
            if (permissions.size() != 0) {
                ActivityCompat.requestPermissions(this, permissions.toArray(new String[0]), REQUEST_CODE);
                return false;
            }
        }
        return true;
    }
}
